package com.example.pufang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.PageParamRequest;
import com.example.pufang.mapper.LoginMapper;
import com.example.pufang.pojo.Login;
import com.example.pufang.service.LoginService;
import com.example.pufang.vo.LoginLogVo;
import com.example.pufang.vo.PageListVo;
import com.example.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public PageListVo getListByPage(PageParamRequest pageParamRequest, String searchName, String searchRealName,
                                    String searchPhone) {
        PageListVo pageListVo = new PageListVo();

        int start = (pageParamRequest.getPage()-1)* pageParamRequest.getLimit() + 1;
        int limit = pageParamRequest.getLimit();

        List<LoginLogVo> logList = loginMapper.getListByPage(start, limit, searchName, searchRealName, searchPhone);

        // 获取总数
        Long total = loginMapper.count(searchName, searchRealName, searchPhone);

        // 获取总页数
        long l = total % pageParamRequest.getLimit();
        long totalPage = 0;
        if (l > 0) {
            totalPage = Math.floorDiv(total, pageParamRequest.getLimit()) + 1;
        } else {
            totalPage = Math.floorDiv(total, pageParamRequest.getLimit());
        }

        pageListVo.setPage((long) pageParamRequest.getPage())
                .setSize((long) pageParamRequest.getLimit())
                .setList(JSONObject.toJSONString(logList))
                .setTotal(total)
                .setTotalPage(totalPage);

        return pageListVo;
    }
}
